package cs131.pa1.filter.sequential;

import java.util.*;

public class SequentialCommandBuilder {
    public static List<SequentialFilter> createFiltersFromCommand(String command) {
        Scanner s = null;
        try {
            s = new Scanner(command);
            s.useDelimiter("\\|");
            List<SequentialFilter> filters = new ArrayList<SequentialFilter>();
            SequentialFilter prev = null;
            while (s.hasNext()) {
                List<String> commands = split(s.next(), ">");
                SequentialFilter filter = StringToFilter(commands.get(0));
                if(filter == null){
                    filters.clear();
                    return null;
                }
                filters.add(filter);
                if (commands.size() == 2) {
                    List<String> redirectionCommands = split(commands.get(1), " ");
                    SequentialFilter redirectionCommand = new RedirectionCommand(redirectionCommands);
                    filters.add(redirectionCommand);
                }

            }
            return filters;
        } finally {
            if (s != null) {
                s.close();
            }
        }

    }



    //to transfer string to the filter
    private static SequentialFilter StringToFilter(String command) {
        List<String> subCommands = split(command, " ");
        SequentialFilter filter = null;
        if (LsCommand.COMMAND.equals(subCommands.get(0))) {
            filter = new LsCommand(subCommands);
        } else if (CdCommand.COMMAND.equals(subCommands.get(0))) {
            filter = new CdCommand(subCommands);
        } else if (PwdCommand.COMMAND.equals(subCommands.get(0))) {
            filter = new PwdCommand(subCommands);
        } else if (CatCommand.COMMAND.equals(subCommands.get(0))) {
            filter = new CatCommand(subCommands);
        } else if (WcCommand.COMMAND.equals(subCommands.get(0))) {
            filter = new WcCommand(subCommands);
        } else if (GrepCommand.COMMAND.equals(subCommands.get(0))) {
            filter = new GrepCommand(subCommands);
        } else if (UniqCommand.COMMAND.equals(subCommands.get(0))) {
            filter = new UniqCommand(subCommands);
        } else {
            System.out.print(cs131.pa1.filter.Message.COMMAND_NOT_FOUND.with_parameter(command));
            return null;
        }

        return filter;

    }

    private static List<String> split(String command, String patten) {
        List<String> commands = new ArrayList<String>();
        try {
            Scanner s = new Scanner(command);
            s.useDelimiter(patten);

            while (s.hasNext()) {
                commands.add(s.next());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return commands;
    }

    private static SequentialFilter determineFinalFilter(String command) {
        return null;
    }

    private static String adjustCommandToRemoveFinalFilter(String command) {
        return null;
    }

    private static SequentialFilter constructFilterFromSubCommand(String subCommand) {
        return null;
    }

    private static boolean linkFilters(List<SequentialFilter> filters) {
        return false;
    }
}
